﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess
{
    public class FS0202DataAccess:FSBase_DataAccess
    {
        #region 检索
        public DataTable Search(string strRequirementNo,string strUserID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("  select a.iAutoID,vcRequirementID,b.vcCategory,b.vcRequirementNo,b.vcRequirementName,b.vcRequirementDescription,vcRequirementContent from(   ");
            strSql.AppendLine("  	select iAutoID,vcRequirementID,vcRequirementContent from tShop    ");
            strSql.AppendLine("  	where 1=1   ");
            strSql.AppendLine("     and ISNULL(vcOrderID,'')=''   ");
            strSql.AppendLine("     and vcUserID = '" + strUserID + "'   ");
            strSql.AppendLine("  )a   ");
            strSql.AppendLine("  left join(   ");
            strSql.AppendLine("  	select iAutoID,vcCategory,vcRequirementNo,vcRequirementName,vcRequirementDescription from tRequirement   ");
            strSql.AppendLine("  )b on a.vcRequirementID = b.iAutoID   ");
            if (!string.IsNullOrEmpty(strRequirementNo))
            {
                strSql.AppendLine("     where b.vcRequirementNo like '" + strRequirementNo + "%'   ");
            }
            return Search(strSql.ToString());
        }
        #endregion

        #region 删除
        public void Delete(List<string> Items)
        {
            StringBuilder strSql = new StringBuilder();
            foreach (var item in Items)
            {
                strSql.AppendLine("   delete tShop where iAutoID = " + item + "  ");
            }
            update(strSql.ToString());
        }
        #endregion

        #region 生成订单
        public void CreateOrder(string strUserID,string strProjectNo,string strOrderNo) 
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("   update tShop set vcProjectNo = '" + strProjectNo + "',vcOrderID = '" + strOrderNo + "' where vcUserID = '" + strUserID + "' and ISNULL(vcOrderID,'')=''  ");
            strSql.AppendLine("   insert into tOrder(vcProjectNo,vcOrderID,vcRequirementID,vcRequirementNo,vcRequirementContent,vcCreateID)   ");
            strSql.AppendLine("   select vcProjectNo,vcOrderID,vcRequirementID,vcRequirementNo,vcRequirementContent,vcUserID from tShop where vcOrderID = '"+strOrderNo+"'  ");
            strSql.AppendLine("   delete tShop where vcOrderID = '" + strOrderNo + "'  ");
            update(strSql.ToString());
        }
        #endregion

        #region 获取最大订单号
        public string getMaxOrderNo()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("   select top 1 vcOrderID from tOrder where ISNULL(vcOrderID,'')<>'' order by iAutoID desc  ");
            DataTable dt = Search(strSql.ToString());
            if (dt!=null && dt.Rows.Count>0 && dt.Rows[0][0]!=null)
            {
                return dt.Rows[0][0].ToString();
            }
            else
            {
                return "";
            }

        }
        #endregion

        #region 获取项目编号列表
        public DataTable getProjectList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("   select distinct vcProjectNo from tProject  ");
            return Search(strSql.ToString());
        }
        #endregion
    }
}
